/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.util;

import java.util.List;

import etcore.webapp.grouptalk.common.object.Directory;
import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.object.Keyword;
import etcore.webapp.grouptalk.common.object.Topic;
import etcore.webapp.grouptalk.common.service.DirectoryService;
import etcore.webapp.grouptalk.common.service.GroupService;
import etcore.webapp.grouptalk.common.service.KeywordService;
import etcore.webapp.grouptalk.common.service.TopicService;

/**
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class SearchUtil {
	/**
	 * Update names' indices of groups and their topics by creating keywords
	 */
	public static void updateAllIndices() {
		GroupService groupService = new GroupService();
		List<Group> result = groupService.findAll();
		if (result != null) {
			for (int i = 0; i < result.size(); i++) {
				Group group = result.get(i);
				updateIndex(group.getId());
			}
		}
	}

	/**
	 * Update names' indices of group and its topics by creating keywords
	 * 
	 * @param groupID
	 *            a group id
	 */
	public static void updateIndex(Integer groupID) {
		if (groupID == null)
			return;
		GroupService groupService = new GroupService();
		Group group = groupService.findById(groupID);

		if (group == null)
			return;
		TopicService topicService = new TopicService();
		DirectoryService directoryService = new DirectoryService();
		// delete indices of the group
		KeywordService keywordService = new KeywordService();
		List<Keyword> keywordList = keywordService.findByProperty("gid",
				groupID);
		keywordService.deleteList(keywordList);

		// update indices of the group
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(group.getName());
		stringBuffer.append(Symbols.COMMA);
		stringBuffer.append(group.getInfo());
		String[] strs = stringBuffer.toString().split(",|，|;|；");
		for (String str : strs) {
			keywordList = keywordService.findBy2Properties("name", str, "gid",
					groupID);
			if (keywordList == null || keywordList.size() == 0) {
				Keyword keyword = new Keyword();
				keyword.setGid(groupID);
				keyword.setName(str.trim());
				keywordService.save(keyword);
			}
		}

		// update indices of the group's topics
		List<Topic> topics = topicService.findByProperty("gid", groupID);
		if (topics != null) {
			for (int k = 0; k < topics.size(); k++) {
				Topic topic = (Topic) topics.get(k);
				String topicName = topic.getName();
				keywordList = keywordService.findBy2Properties("name",
						topicName, "gid", groupID);
				if (keywordList == null || keywordList.size() == 0) {
					Keyword keyword = new Keyword();
					keyword.setGid(groupID);
					keyword.setTid(topic.getId());
					keyword.setName(topicName);
					keywordService.save(keyword);
				}
			}
		}

		//
		List<Directory> directories = directoryService.findByGid(groupID);
		if (directories != null) {
			for (int k = 0; k < directories.size(); k++) {
				Directory directory = directories.get(k);
				String directoryName = directory.getName();
				keywordList = keywordService.findBy2Properties("name",
						directoryName, "gid", groupID);
				if (keywordList == null || keywordList.size() == 0) {
					Keyword keyword = new Keyword();
					keyword.setGid(groupID);

					keyword.setName(directoryName);
					keywordService.save(keyword);
				}

			}
		}

	}

	/**
	 * Find keywords using name
	 * 
	 * @param inputKey
	 *            input key
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List getKeywords(String inputKey) {
		KeywordService service = new KeywordService();
		List result = service.searchByProperty("name", inputKey, 0, 10);
		return result;

	}

}
